package com.tgsoft.common.core.runner;


import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author MrBird
 */
@Component
@RequiredArgsConstructor
public class StartedUpRunner implements ApplicationRunner {

    private final ConfigurableApplicationContext context;
    private final Environment environment;

    @Override
    public void run(ApplicationArguments args) {
        if (context.isActive()) {
            String banner = "-----------------------------------------\n" +
                    "服务名称：" + environment.getProperty("spring.application.name") + "\n" +
                    "端口号：" + environment.getProperty("server.port") + "\n" +
                    "-----------------------------------------";
            printSingleColor(95,40,3,banner);

            //System.out.format("输出日志");//%n表示换行
        }
    }


    /**
     * %s是字符串占位符，%d 是数字占位符
     * 格式：System.out.println("\33[前景色代号;背景色代号;数字m")
     *
     * @param frontcode    颜色代号：前景色代号(30-37)。   90-97  比颜色1更鲜艳一些
     *                     30白色；31红色；32绿色；33黄色；34蓝色；35紫色；36浅蓝；37灰色
     * @param backcode     颜色代号：背景颜色代号(40-47) 和颜色顺序相同
     * @param fontcode     数字+m：0空样式；1加粗；3斜体；4下划线；7反色
     * @param content 要打印的内容
     */
    public static void printSingleColor(int frontcode,int backcode,int fontcode, String content) {
        //System.out.println();
        //System.out.format("\33[%d;%d;%dm%s%n",frontcode, backcode,fontcode, content);
        //System.out.format("\33[%d;%d;%dm%s%n");
        //System.out.println("\33["+frontcode+";"+backcode+";"+fontcode+"m"+content+ "\033[0m");
        //  System.out.println("\33["+frontcode+";"+backcode+";"+fontcode+"m"+content+ "\033[0m");
        System.out.println("\33["+frontcode+";"+fontcode+"m"+content+ "\033[0m");

    }
}
